//
//  SPCAlbumPhotoListViewController.swift
//  aj
//
//  Created by leeqb on 2/11/16.
//  Copyright © 2016 powerlee. All rights reserved.
//

import UIKit

class SPCAlbumPhotoListViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout, BottomButtonDelegate {
    var isEdit = false
    var zoneId: Int!
    var editButtonsView: BottomButton!
    var sectionDataList: [NSDictionary]?
    
    // MARK: - Overrides
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false
        self.collectionView?.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 10)
        self.collectionView?.delaysContentTouches = false
        
        self.requestData()
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        
        if editButtonsView != nil {
            isEdit = false
            editButtonsView.hide()
            editButtonsView = nil
        }
    }
    
    // MARK: - 服务器请求
    private func requestData() {
        if zoneId != nil && zoneId > 0 {
            let params = ["zoneId": zoneId, "currentPage": 0, "pageSize": 100]
            CBMessage.shareInstance().showLoading()
            AJNetworkHelper.instance.post("aj_my_zone_photo_list",
                parameters: params,
                success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) -> Void in
                    self.sectionDataList = responseObject["result"]!!["list"] as? [NSDictionary]
                    self.collectionView?.reloadData()
                }, failed: { (operation: AFHTTPRequestOperation!, error: NSError!) -> Void in
                }) { () -> Void in
                    CBMessage.shareInstance().dismiss()
            }
        }
    }
    
    // MARK: - UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
        var cell: UICollectionReusableView!
        if kind == UICollectionElementKindSectionHeader {
            cell = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "SectionHeader", forIndexPath: indexPath)
            
            let sectionData = sectionDataList![indexPath.row]
            let dateLabel = cell.viewWithTag(1) as! UILabel
            let checkImageView = cell.viewWithTag(2) as! UIImageView
            dateLabel.text = sectionData["date"] as? String
            
            if isEdit {
                checkImageView.hidden = false
            } else {
                checkImageView.hidden = true
            }
        }
        return cell
    }
    
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        let avgWidth = (SCREEN_WIDTH - 10) / 3
        return CGSize(width: avgWidth, height: avgWidth * 0.8)
    }
    
    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return self.sectionDataList?.count ?? 0
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if sectionDataList?.count > 0 {
            let photoList = sectionDataList?[section]["photoPreDayList"] as? [NSDictionary]
            return photoList?.count ?? 0
        }
        
        return 0
    }

    /*
    {isPrivate = 1;
    photo = "http://mymost.gotoip55.com/service/img?img=/clt/1456360381219.jpg";
    photoId = 114;}
    */
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("PhotoCell", forIndexPath: indexPath)
        
        let photoList = sectionDataList?[indexPath.section]["photoPreDayList"] as? [NSDictionary]
        let rowData = photoList![indexPath.row]
        let imageView = cell.viewWithTag(1) as! UIImageView
        let checkImageView = cell.viewWithTag(2) as! UIImageView
        imageView.sd_setImageWithURL(NSURL(string: rowData["photo"] as? String ?? ""), placeholderImage: g_defaultImage)
        
        if isEdit {
            checkImageView.hidden = false
        } else {
            checkImageView.hidden = true
        }
        
        return cell
    }

    // MARK: - Button Clicks
    @IBAction func editClick(sender: UIButton) {
        if isEdit == true {
            isEdit = false
            editButtonsView.hide()
            editButtonsView = nil
        } else {
            isEdit = true
            editButtonsView = BottomButton.getInstance()
            editButtonsView.delegate = self
            editButtonsView.buttons = [["title": "取消分享", "image": "ic_cancel_share_n"],
                ["title": "取消全部分享", "image": "ic_cancel_share_all_n"]
            ]
            editButtonsView.show()
        }
        
        self.collectionView?.reloadData()
    }
    
    @IBAction func shareClick(sender: UIButton) {
    }
    
    // MARK: - BottomButtonDelegate
    func bottomButtonClick(index: Int) {
        
    }
    
}
